package cn.zaier.javafx.controlle;

import cn.hutool.extra.spring.SpringUtil;
import cn.zaier.javafx.Constants;
import javafx.concurrent.Task;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @desc 清空文件目录的任务类
 */
public class FileClearWork extends Task<List<String>>  {
    public  String fileDir; // 扫描文件目录
    public  String fileTypes; // 文件类型
    public  long longtime; //  执行间隔

    public  Map<String,Object> extArgs;// 扩展其它参数


    public  ArrayList<String> resultFileName=new ArrayList<String>();


    public  final InitSceneViewControlle initSceneViewControlle;


    public FileClearWork(String fileDir, String fileTypes, long longtime, Map<String,Object> extArgs) {
        this.fileDir = fileDir;
        this.fileTypes = fileTypes;
        this.longtime = longtime;
        this.extArgs= extArgs;
        this.initSceneViewControlle =SpringUtil.getBean(InitSceneViewControlle.class);
    }

    @Override
    public List<String> call()  {
        try {
            if (this.isCancelled()){
                return Collections.EMPTY_LIST;
            }
            initSceneViewControlle.printLog("清理任务","当前清理目录: "+fileDir);
            if (resultFileName.size()>0)resultFileName.clear();
            if (StringUtils.isBlank(fileDir))return null;
            boolean contains = fileDir.contains("\n");
            if (contains){
                String[] split = getFileStrings();
                for (String s : split) {
                    File file = new File(s);
                    if (!file.exists()){
                        initSceneViewControlle.printLog("错误",file.getAbsolutePath()+"文件不存在!!!");
                    }
                    recursionFile(file,fileTypes);
                }
            }else {
                File file = new File(fileDir);
                recursionFile(file,fileTypes);
            }
            InitSceneViewControlle.count.addAndGet(1);
            System.out.println("完成清理次数"+InitSceneViewControlle.count.get());
            initSceneViewControlle.printLog("清理任务","完成清理次数"+InitSceneViewControlle.count.get());
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (this.resultFileName.size()==0){
                initSceneViewControlle.printLog("清理任务","当前清理已完成,已经没有文件了");
            }
            return resultFileName;
        }

    }

    private String[] getFileStrings() {
        String[] split = fileDir.split("\n");
        return split;
    }

    public void recursionFile(File file,String fileTypes){
            // 不删除指定的目录
            // 文件不存在; 根目录;
            if (!file.exists()|| (Arrays.stream(getFileStrings()).collect(Collectors.toList()).contains(file.getAbsolutePath()) && file.listFiles().length == 0)){
                return;
            }
            // 非指定的文件后缀
            if (!file.isDirectory() && StringUtils.isNotBlank(fileTypes) &&
                    Arrays.stream(fileTypes.split(",")).collect(Collectors.toList())
                            .stream().filter(item->file.getName().toUpperCase().endsWith(item.toUpperCase()))
                            .collect(Collectors.toList()).size() == 0){
                return;
            }
            if (!file.isDirectory() || file.listFiles().length==0){
                delete(file);
            }else{
                for (File listFile : file.listFiles()) {
                    recursionFile(listFile,fileTypes);
                }
            }
    }



    private boolean delete(File file) {
        // 文件修改时间
        long fileLastModified = file.lastModified();
        if (extArgs.containsKey(Constants.FILE_UPDATE_TIME_BEFORE)
                && new Date().getTime() - fileLastModified < 1000 * 60 * Long.parseLong(extArgs.get(Constants.FILE_UPDATE_TIME_BEFORE).toString()) ){ //  分钟
            return false;
        }

        return  checkdel(file);
    }

    private  boolean checkdel(File file) {
        if (file.exists()){
            resultFileName.add(file.getAbsolutePath());
            return file.delete();
        }
        return false;
    }


}
